/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-8-20         luwei
 */

package com.tianci.appstore.ui.list.manage.more;

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.ui.control.MoreManageController;
import com.tianci.appstore.ui.control.SkyMainPageController;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.ui.mainpage.SkyMyAppPage;
import com.tianci.appstore.ui.widget.SkyAppPager.Direction;
import com.tianci.appstore.ui.widget.SkyCategoryBar;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyCustomLayout.BlockData;
import com.tianci.appstore.ui.widget.SkyGridAdapter;
import com.tianci.appstore.ui.widget.SkyGridAdapter.AdapterType;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class MoreManagePage extends SkyMyAppPage {
    private SkyCategoryBar mCategoryBar = null;

    public MoreManagePage(Context context, Direction direction) {
        super(context, direction);
        addCategoryBar();
        mController.refreshContent(SkyMainPageController.getMoreManageData());
        mController.reSetFocus();
    }

    @Override
    protected void initController() {
        mController = new MoreManageController(mContext);
    }

    @Override
    protected SkyCustomLayout initMyAppPage() {
        SkyCustomLayout layout = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
        layout.setBlockNum(3, 3);
        layout.setBlockSize(SkyConstants.Div(608), SkyConstants.Div(281));
        layout.setVerticalSpace(SkyConstants.Div(-50));
        layout.setHorizontalSpace(SkyConstants.Div(-50));
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);
        layout.setMargin(SkyConstants.Div(97), SkyConstants.Div(232), SkyConstants.Div(0), 0);
        layout.setBlockDatas(createListBlockData(3, 3));
        SkyGridAdapter mAdapter = new SkyGridAdapter(mContext, CoocaaApplication.mDiv, AdapterType.MANAGE_TYPE);
        mAdapter.setKeyListener(mController);
        layout.setAdapter(mAdapter);
        mController.setAppPager(mPager);
        return layout;
    }

    private void addCategoryBar() {
        mCategoryBar = new SkyCategoryBar(mContext);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                SkyConstants.Div(1400), SkyConstants.Div(200));
        params.setMargins(SkyConstants.Div(110), SkyConstants.Div(85), 0, 0);
        mCategoryBar.setLayoutParams(params);
        mCategoryBar.setCategoryChangeListener(mController);
        addView(mCategoryBar);
        mCategoryBar.setFocusable(false);
        mCategoryBar.setFocusableInTouchMode(false);
        mCategoryBar.setCategoryDatas(initDefaultData());
        mCategoryBar.setSelected(true);
    }

    private List<SkyCategoryData> initDefaultData() {
        List<SkyCategoryData> datas = new ArrayList<SkyCategoryData>();
        SkyCategoryData data = new SkyCategoryData("", "更多功能");
        datas.add(data);
        return datas;
    }

    private List<BlockData> createListBlockData(int row, int column) {
        List<BlockData> datas = new ArrayList<BlockData>();
        for (int i = 0; i < row * column; i++) {
            BlockData data = new BlockData();
            data.mColNum = 1;
            data.mRowNum = 1;
            data.mBlock = new int[]{i};
            boolean leftTurn = false;
            boolean rightTurn = false;
            if (i % column == 0)
                leftTurn = true;
            if (i % column == column - 1)
                rightTurn = true;
            data.mTurnPage = new boolean[]{leftTurn, false, rightTurn, false};
            datas.add(data);
        }
        return datas;
    }

    public IAudio getAudioInterface() {
        return mController;
    }
}
